package cn.kent.simple;

import java.util.Arrays;

/**
 * 344. 反转字符串
 */
public class ReverseString {
    public static void main(String[] args) {
        char[] s = {'h', 'e', 'l', 'l', 'o'};
        reverseString3(s);
        System.out.println(Arrays.toString(s));
    }

    /**
     * 递归
     */
    public static void reverseString3(char[] s) {
        cycle(s, 0, s.length - 1);
    }

    public static void cycle(char[] s, int l, int r) {
        if (l <= r) {
            cycle(s, l + 1, r - 1);
            swap(s, l, r);
        }
    }

    public static void swap(char[] s, int a, int b) {
        char tmp = s[a];
        s[a] = s[b];
        s[b] = tmp;
    }

    /**
     * 双指针反转
     */
    public static void reverseString2(char[] s) {
        int len = s.length;
        for (int l = 0, r = len - 1; l < r; l++, r--) {
            char tmp = s[l];
            s[l] = s[r];
            s[r] = tmp;
        }
    }

    /**
     * 原地反转
     */
    public static void reverseString(char[] s) {
        int len = s.length;
        int mid = len / 2;
        len--;
        for (int i = 0; i < mid; i++) {
            char tmp = s[i];
            s[i] = s[len];
            s[len] = tmp;
            len--;
        }
    }
}
